﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.Models;
using QLBH.Modules;
using QLBH.Utilities;

namespace QLBH.Controllers
{
    public class StaffController
    {
        private StaffModel ObjModel;
        private ucStaffs ViewStaffs;
        public StaffController(ucStaffs ViewStaffs)
        {
            ObjModel = new StaffModel();
            this.ViewStaffs = ViewStaffs;
        }

        public void LoadData()
        {
            try
            {
                List<StaffModel> data=WSAccess.GetData<List<StaffModel>>(MainForm.Instance.Shop_id.ToString()+"/staffs");

                ViewStaffs.DisplayData(data);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        internal void ChangerPassWord()
        {
            frmChange_Pasword Frmchanger = new frmChange_Pasword();
            if (Frmchanger.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }
        public void Add()
        {
            frmAdd_Edit_Staff FrmStaff = new frmAdd_Edit_Staff(eType.ADD);
            FrmStaff.Text = "Đăng ký tài khoản";
            if(FrmStaff.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }

        public void Edit()
        {
            frmAdd_Edit_Staff FrmStaff = new frmAdd_Edit_Staff(eType.EDIT);
            FrmStaff.Text = "Thay đổi thông tin";
            try
            {
                if (ViewStaffs.Selectdata() != null)
                {
                    FrmStaff.ObjModel = ViewStaffs.Selectdata();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"Lỗi",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            if (FrmStaff.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }
        internal void Delete()
        {
            if (ViewStaffs.Selectdata() != null)
            {
                ObjModel = ViewStaffs.Selectdata();
                if (MessageBox.Show("Bạn muốn xóa người dùng " + ObjModel.Name, "Xóa", MessageBoxButtons.OKCancel,MessageBoxIcon.Question) == DialogResult.OK)
                {
                    try
                    {
                        WSAccess.Delete<StaffModel>(ObjModel.Id + "/staff/delete", ObjModel);
                        LoadData();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show("Bạn phải chọn dữ liệu trước khi xóa");
            }

        }
    }
}
